package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class  ConfirmAndViewOrderServlet extends HttpServlet {
    public static final String VIEW_ORDER = "/WEB-INF/jsp/order/ViewOrder.jsp";
    private Order order;
    private Integer currentOrderId;
    private List<Order> orderList;
    private OrderService orderService;
    private int nextId;
    private LogService logService;
    private Account account;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        order = (Order) session.getAttribute("order");

        orderService = new OrderService();
        orderService.insertOrder(order);
        session.setAttribute("order", order);

        session.setAttribute("cart",null);
        account = (Account)session.getAttribute("account");
        logService = new LogService();
        logService.insertLog("Confirm Order","OrderId:"+String.valueOf(order.getOrderId()), account.getUsername());

        request.getRequestDispatcher(VIEW_ORDER).forward(request,response);
    }
}
